import 'package:flutter/material.dart';

class CustomLinearProgressBar extends StatelessWidget {
  const CustomLinearProgressBar({
    this.backgroundColor = Colors.transparent,
    this.valueColor = Colors.red,
    this.value,
  }) : assert(value != null);
  final Color backgroundColor;
  final Color valueColor;
  final double value;

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: const BoxConstraints(
        minWidth: double.infinity,
        minHeight: 6.0,
      ),
      child: CustomPaint(
        painter: _CustomLinearProgressBarPainter(
          backgroundColor: backgroundColor,
          valueColor: valueColor,
          value: value,
        ),
      ),
    );
  }
}

class _CustomLinearProgressBarPainter extends CustomPainter {
  const _CustomLinearProgressBarPainter({
    this.backgroundColor,
    this.valueColor,
    this.value,
  });

  final Color backgroundColor;
  final Color valueColor;
  final double value;

  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..color = backgroundColor
      ..style = PaintingStyle.fill;
    final Rect trackRect = Offset.zero & size;
    canvas.drawRect(trackRect, paint);
    canvas.clipRect(trackRect);

    paint
      ..color = valueColor
      ..strokeCap = StrokeCap.round
      ..isAntiAlias = true
      ..strokeWidth = size.height;

    void drawBar(double x, double width) {
      if (width <= 0.0) return;
      double y = size.height / 2.0;
      canvas.drawLine(Offset(x, y), Offset(width, y), paint);
      //canvas.drawRect(Offset(left, 0.0) & Size(width, size.height), paint);
    }

    drawBar(0.0, value.clamp(0.0, 1.0) * size.width as double);
  }

  @override
  bool shouldRepaint(_CustomLinearProgressBarPainter oldPainter) {
    return oldPainter.backgroundColor != backgroundColor ||
        oldPainter.valueColor != valueColor ||
        oldPainter.value != value;
  }
}
